package com.ttj.service.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ttj.domain.oracle.OrderInstock;
import com.ttj.domain.oracle.OrderInstockDetail;
import com.ttj.domain.oracle.OrderRec;
import com.ttj.mapper.oracle.BUserMapper;
import com.ttj.mapper.oracle.OrderInstockDetailMapper;
import com.ttj.mapper.oracle.OrderInstockMapper;
import com.ttj.mapper.oracle.OrderRecMapper;
import com.ttj.service.PickGoodService;
import com.ttj.util.BeanUtil;
import com.ttj.vo.BaseRes;

@Service
public class PickGoodServiceImpl implements PickGoodService {

	@Resource
	private OrderRecMapper orderRecMapper;

	@Resource
	private OrderInstockMapper orderInstockMapper;

	@Resource
	private BUserMapper bUserMapper;

	@Resource
	private OrderInstockDetailMapper instockDetailMapper;

	@Override
	public BaseRes<List<OrderInstock>> pickedGood(String username,
			Integer oStatus) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("creater", username);
		map.put("oStatus", oStatus);
		List<OrderInstock> pickedGood = orderInstockMapper.pickedGood(map);
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
		for (OrderInstock orderInstock : pickedGood) {
			String oindate = orderInstock.getoIndate() == null ? "" : sdf
					.format(orderInstock.getoIndate());
			String deliveryDate = orderInstock.getDeliveryDate() == null ? ""
					: sdf.format(orderInstock.getDeliveryDate());
			String valdateDate = orderInstock.getValdateDate() == null ? ""
					: sdf.format(orderInstock.getValdateDate());
			String hangDate = orderInstock.getHangDate() == null ? "" : sdf
					.format(orderInstock.getHangDate());
			orderInstock.setOindate(oindate);
			orderInstock.setDeliveryTime(deliveryDate);
			orderInstock.setValdateTime(valdateDate);
			orderInstock.setHangTime(hangDate);
		}
		BaseRes<List<OrderInstock>> baseRes = new BaseRes<List<OrderInstock>>();
		baseRes.setData(BeanUtil.voConvertList(pickedGood, OrderInstock.class));
		return baseRes;
	}

	@Override
	public BaseRes<List<OrderInstock>> unPickGood(String username) {
		Map<String, Object> map = new HashMap<String, Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
		map.put("creater", username);
		List<OrderInstock> unPickGood = orderInstockMapper.unPickGood(username);
		for (OrderInstock orderInstock : unPickGood) {
			String indate = orderInstock.getoIndate() == null ? "" : sdf
					.format(orderInstock.getoIndate());
			orderInstock.setOindate(indate);
		}
		BaseRes<List<OrderInstock>> baseRes = new BaseRes<List<OrderInstock>>();
		baseRes.setData(BeanUtil.voConvertList(unPickGood, OrderInstock.class));
		return baseRes;
	}

	@Override
	public BaseRes<List<OrderInstock>> instockDetail(Long orderId) {
		List<OrderInstock> instockDetail = orderInstockMapper
				.instockDetail(orderId);
		BaseRes<List<OrderInstock>> baseRes = new BaseRes<List<OrderInstock>>();
		baseRes.setData(instockDetail);
		return baseRes;
	}

	@Override
	public BaseRes<List<OrderInstock>> instockDetailList(Long orderId) {
		List<OrderInstock> detailList = orderInstockMapper
				.instockDetailList(orderId);
		BaseRes<List<OrderInstock>> baseRes = new BaseRes<List<OrderInstock>>();
		baseRes.setData(detailList);
		return baseRes;
	}

	@Override
	public int reply(String orderId) {
		String[] orderIds = orderId.split(",");
		int reply = 0;
		for (String string : orderIds) {
			reply = orderInstockMapper.reply(string);
		}
		return reply;
	}

	@Override
	public Map<String, Object> driverPicked(String username, String password,
			Long orderId) {
		Map<String, Object> map = new HashMap<String, Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		map.put("username", username);
		map.put("password", password);
		map.put("orderId", orderId);
		int checkLogin = bUserMapper.checkLogin(map);
		if (checkLogin == 1) {
			map.remove("password");
			map.put("time", sdf.format(new Date()));
			orderInstockMapper.driverPicked(map);
			map.clear();
			map.put("res", "拉货完成");
		} else {
			map.clear();
			map.put("res", "账号密码错误或该订单不是该司机拉货");
		}
		return map;
	}

	@Override
	public int delete(String orderId) {
		String[] strings = orderId.split(",");
		int delete = 0;
		for (String string : strings) {
			instockDetailMapper.delete(string);
			delete += orderInstockMapper.delete(string);
		}
		return delete;
	}

	@Override
	public int repeal(String orderId) {
		String[] orderIds = orderId.split(",");
		int repeal = 0;
		for (String string : orderIds) {
			repeal = orderInstockMapper.repeal(string);
		}
		return repeal;
	}

	@Override
	public Map<String, Object> create(List<OrderInstock> listInstock,
			String username) {
		OrderInstock instock = BeanUtil.voConvert(listInstock,
				OrderInstock.class);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> map1 = new HashMap<String, Object>();
		map.put("username", username);
		Calendar ca = Calendar.getInstance();
		int year = ca.get(Calendar.YEAR);
		int month = ca.get(Calendar.MONTH) + 1;
		int day = ca.get(Calendar.DATE);
		String begin = year + "-" + month + "-" + day + " 00:00:00";
		map.put("begin", begin);
		int checkOrder = orderInstockMapper.checkOrder(map);// 检测当天是否已经创建拉货订单
		if (checkOrder == 0) {
			Date oindate = orderInstockMapper.getLastOindate(username);// 获取用户最后一次拉货时间
			String oIndate = "";
			if(oindate!=null){
				oIndate = sdf.format(oindate);
			}else{
				oIndate = "2015-01-01 00:00:01";
			}
			int maxInId = orderInstockMapper.getMaxInId();
			int maxOrderId = orderInstockMapper.getMaxOrderId();
			int sid = bUserMapper.getBranch(username);
			List<OrderRec> orderRecList = orderRecMapper.getOrderIds(oIndate);
			String orderIds = "";
			for (OrderRec orderRec : orderRecList) {
				if (orderRec.getOrderId() != null) {
					orderIds += "," + orderRec.getOrderId();
				} else {
					orderIds = null;
				}
			}
			instock.setOrderIds(orderIds);// 订单集合
			instock.setInId(maxInId + 1);// 入库单号
			instock.setOrderId(maxOrderId + 1);// 订单号
			instock.setoIndate(new Date());// 创建时间
			instock.setModifTime(new Date());//修改时间
			instock.setSid(sid);// 网点
			instock.setCreater(username);// 创建人
			orderInstockMapper.createInstock(instock);// 入库表
			OrderInstockDetail instockDetail = new OrderInstockDetail();
			for (OrderInstock orderInstock : listInstock) {
				int gId = orderInstock.getGid();
				int num = orderInstock.getNum();
				double weight = orderInstock.getWeight();
				instockDetail.setOrderId(maxOrderId + 1);
				instockDetail.setgId(gId);
				instockDetail.setNum(num);
				instockDetail.setWeight(weight * 1000);
				instockDetail.setInId(maxInId + 1);
				instockDetail.setoIndate(new Date());
				instockDetailMapper.createInstockDetail(instockDetail);
			}
			map1.put("res", "创建成功");
			return map1;
		} else {
			map1.put("res", "当天只能创建一次订单");
			return map1;
		}
	}

	@Override
	public Map<String, Object> backOrder(List<OrderInstock> listInstock,
			String orderId) {
		Map<String, Object> map = new HashMap<String, Object>();
		OrderInstock instock = orderInstockMapper.instock(orderId);
		OrderInstockDetail instockDetail = new OrderInstockDetail();
		Integer inId = instock.getInId();
		for (OrderInstock orderInstock : listInstock) {
			int gId = orderInstock.getGid();
			int num = orderInstock.getNum();
			double weight = orderInstock.getWeight();
			instockDetail.setOrderId(Integer.parseInt(orderId));
			instockDetail.setgId(gId);
			instockDetail.setNum(num);
			instockDetail.setWeight(weight * 1000);
			instockDetail.setInId(inId);
			instockDetail.setoIndate(new Date());
			instockDetailMapper.createInstockDetail(instockDetail);
		}
		map.put("res", "补货完成");
		return map;
	}

	@Override
	public OrderInstock instock(String orderId) {
		return orderInstockMapper.instock(orderId);
	}

}
